[XSL-FO] Characters from other than English languages

Posted by Lukasz Kurylo on Geeks with Blogs See other posts from Geeks with Blogs or by Lukasz Kurylo
Published on Sun, 07 Oct 2012 20:08:54 GMT Indexed on 2012/10/09 15:40 UTC
Read the original article Hit count: 312

Filed under:

My client have departments in Europe Central and East, so there is highly possibility that in the generated pdfs there will be at least in the people names and/or surnames some specific characters for the country language.

 

With the XSL-FO we can use some out-of-the box fonts, e.g. the default is Times. We can change it for specific block of text or the entire document to other like Helvetica or Arial. All will be good to the moment that we use only an english alphabet. If we want to add e.g. some characters from polish or bulgarian language, in the *.fo file:

 

      <fo:block >

                <fo:inline font-weight="bold">english: </fo:inline>

                <fo:inline font-weight="bold">yellow</fo:inline>

      </fo:block>

      <fo:block>

                <fo:inline font-weight="bold">polish: </fo:inline>

                <fo:inline font-weight="bold">zólty</fo:inline>

      </fo:block>

      <fo:block>

                <fo:inline font-weight="bold">russian: </fo:inline>

                <fo:inline font-weight="bold">??????</fo:inline>

      </fo:block>

      <fo:block>

                <fo:inline font-weight="bold">bulgarian: </fo:inline>

                <fo:inline font-weight="bold">????</fo:inline>

      </fo:block>

      <fo:block>

                <fo:inline font-weight="bold">english: </fo:inline>

                <fo:inline font-weight="bold">yellow</fo:inline>

      </fo:block>

      <fo:block>

                <fo:inline font-weight="bold">polish: </fo:inline>

                <fo:inline font-weight="bold"  font-family="Arial">zólty</fo:inline>

      </fo:block>

      <fo:block>

                <fo:inline font-weight="bold">russian: </fo:inline>

                <fo:inline font-weight="bold" font-family="Arial">??????</fo:inline>

      </fo:block>

      <fo:block>

                <fo:inline font-weight="bold">bulgarian: </fo:inline>

                <fo:inline font-weight="bold" font-family="Arial">????</fo:inline>

      </fo:block>

 

The result can be diffrent from the expected depending on the selected font, e.g:

 

yellow

 

 

 

 

 

 

 



As you can see Timer nor Arial work in this case.

 

The problem here is not related to XSL-FO, but rather to the renderer we are using.

I have lost a lot of time to find a solution for the using by me XSL-FO –> PDF rendered to acquire these characters in my generated files. Fortunatelly all what have to be done it is to embed the font (or part of it) in the file(s) during rendering.

 

The renderer that I’m using it is an open source FO.NET.

 

For this one, the code to generate a pdf file looks that:

 

var fonet =  Fonet.FonetDriver.Make();

fonet.Render("source.fo", "result.pdf");

 

To emded the font in the pdf, we need to set the appropriate option to the driver:

 

fonet.Options = new Fonet.Render.Pdf.PdfRendererOptions()

{

      FontType = Fonet.Render.Pdf.FontType.Embed

};


Right now, the pdf we get should look like this:

 

work

 

 

 

 

 

 



As you can see, the result for the Arial font looks exactly how it should, because this font has a characters included not only for the english language like the default Times, which we shouls avoid if we not generating a english-only documents.

 

This is worth to notice that in this situation the generated pdf file is quite large, it has more than 400 kb in size. This is of course because of embedding the entire font in it to make the document portable to systems, where the used font is not present.

Instead on embedding the entire font, we can only embed the subset of used characters by changing the options to:

 

fonet.Options = new Fonet.Render.Pdf.PdfRendererOptions()

{

      FontType = Fonet.Render.Pdf.FontType.Subset

};

 

Right now, this specific pdf is only 12 kb in size.

© Geeks with Blogs or respective owner